package com.pap.product.publish.agent.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.tamper.BeanCopy;
import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.business.StatusFlagEnum;
import com.pap.obj.vo.utils.collection.CollectionUtilss;
import com.pap.product.agent.publish.IProductPublishAgent;
import com.pap.product.agent.publish.dto.ProductPublishAreaDTO;
import com.pap.product.agent.publish.dto.ProductPublishCarDTO;
import com.pap.product.agent.publish.dto.ProductPublishWithDetailDTO;
import com.pap.product.product.auto.entity.Product;
import com.pap.product.product.auto.mapper.ProductMapper;
import com.pap.product.publish.auto.entity.ProductPublish;
import com.pap.product.publish.auto.entity.ProductPublishDetailArea;
import com.pap.product.publish.auto.entity.ProductPublishDetailCar;
import com.pap.product.publish.auto.mapper.ProductPublishDetailAreaMapper;
import com.pap.product.publish.auto.mapper.ProductPublishDetailCarMapper;
import com.pap.product.publish.auto.mapper.ProductPublishMapper;

@Transactional
@Service("productPublishAgent")
public class ProductPublishAgentImpl extends PapBaseServiceImpl<ProductPublish> implements IProductPublishAgent {

	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private ProductPublishMapper productPublishMapper;
	
	@Autowired
	private ProductPublishDetailAreaMapper productPublishDetailAreaMapper;
	
	@Autowired
	private ProductPublishDetailCarMapper productPublishDetailCarMapper;
	
	
	@Override
	public int saveOrUpdateWithDetails(ProductPublishWithDetailDTO publishWithDetailDTO, String clientLicenseId, String loginUserId) {
		String inputPublishId = publishWithDetailDTO.getId();
		if(StringUtilss.isEmpty(inputPublishId)) {
			inputPublishId = UUID.randomUUID().toString();
		}
		
		productPublishDetailAreaMapper.deleteByPublishId(publishWithDetailDTO.getId());
		productPublishDetailCarMapper.deleteByPublishId(publishWithDetailDTO.getId());
		
		ProductPublish databaseProductPublish = new ProductPublish();
		BeanCopy.create(ProductPublishWithDetailDTO.class, ProductPublish.class).copy(publishWithDetailDTO, databaseProductPublish);
		if(StringUtilss.isNotEmpty(databaseProductPublish.getId())) {
			databaseProductPublish.setModifyIp("0.0.0.0");
			databaseProductPublish.setModifyTime(DateUtils.getCurrDateTimeStr());
			databaseProductPublish.setModifyUser(loginUserId);
			mapper.updateByPrimaryKeySelective(databaseProductPublish);
		} else {
			databaseProductPublish.setId(inputPublishId);
			databaseProductPublish.setStatusFlag(StatusFlagEnum.YES.getKey());
			databaseProductPublish.setClientLicenseId(clientLicenseId);
			databaseProductPublish.setCreateUser(loginUserId);
			databaseProductPublish.setCreateIp("0.0.0.0");
			databaseProductPublish.setCreateTime(DateUtils.getCurrDateTimeStr());
			mapper.insertSelective(databaseProductPublish);
		}
		
		List<ProductPublishAreaDTO> areaDTOList = publishWithDetailDTO.getAreaDetails();
		if(areaDTOList != null && areaDTOList.size() > 0) {
			for (ProductPublishAreaDTO productPublishAreaDTO : areaDTOList) {
				ProductPublishDetailArea temp = new ProductPublishDetailArea();
				temp.setId(UUID.randomUUID().toString());
				temp.setAreaIds(CollectionUtilss.join(productPublishAreaDTO.getValue(), ","));
				temp.setProductPublishId(inputPublishId);
				temp.setProductId(publishWithDetailDTO.getProductId());
				temp.setClientLicenseId(clientLicenseId);
				temp.setCreateUser(loginUserId);
				temp.setCreateIp("0.0.0.0");
				temp.setCreateTime(DateUtils.getCurrDateTimeStr());
				productPublishDetailAreaMapper.insertSelective(temp);
			}
		}
		
		List<ProductPublishCarDTO> carDTOList = publishWithDetailDTO.getCarDetails();
		if(carDTOList != null && carDTOList.size() > 0) {
			for (ProductPublishCarDTO productPublishCarDTO : carDTOList) {
				ProductPublishDetailCar temp = new ProductPublishDetailCar();
				temp.setId(UUID.randomUUID().toString());
				temp.setCarIds(CollectionUtilss.join(productPublishCarDTO.getValue(), ","));
				temp.setProductPublishId(inputPublishId);
				temp.setProductId(publishWithDetailDTO.getProductId());
				temp.setClientLicenseId(clientLicenseId);
				temp.setCreateUser(loginUserId);
				temp.setCreateIp("0.0.0.0");
				temp.setCreateTime(DateUtils.getCurrDateTimeStr());
				productPublishDetailCarMapper.insertSelective(temp);
			}
		}

		return 1;
		
	}


	@Override
	public int deleteWithDetails(String productPublishId) {
		productPublishMapper.deleteByPublishId(productPublishId);
		productPublishDetailAreaMapper.deleteByPublishId(productPublishId);
		productPublishDetailCarMapper.deleteByPublishId(productPublishId);
		return 1;
	}


	@Override
	public List<Product> selectProductByAreaAndCar(String areaIds, String carIds) {
		List<Product> returnProductList = new ArrayList<Product>();
		List<String> productIds = productPublishMapper.selectProductIdsByAreaAndCarIds(areaIds, carIds);
		if(productIds != null && productIds.size() > 0) {
			for (String string : productIds) {
				Product temp = productMapper.selectByPrimaryKey(string);
				returnProductList.add(temp);
			}
		}
		return returnProductList;
	}

}
